package app

import (
	"reflect"
	"hub.hddznet.com/app/framework/tools"
)

var beans = make([]interface{}, 0)

func RegistBean(bean interface{}) {
	beans = append(beans, bean)
}
func Bean(bean interface{}) interface{} {
	if reflect.TypeOf(bean).Kind() != reflect.Ptr || reflect.ValueOf(bean).Pointer() == 0 {
		panic("输入的bean对象必须为一个指针值，并且不能是nil对象")
	}
	for _, e := range beans {
		if tools.RftRealType(e).PkgPath() != tools.RftRealType(bean).PkgPath() {
			continue
		}
		if reflect.TypeOf(e).Kind() == reflect.Ptr {
			reflect.ValueOf(bean).Elem().Set(reflect.ValueOf(e).Elem())
			return bean
		} else {
			reflect.ValueOf(bean).Elem().Set(reflect.ValueOf(e))
			return bean
		}
	}
	panic("当前bean对象未注册")
}
